from download_util import cached_download, get_moved_file

import os, json, sys

VERSION_MANIFEST = 'https://launchermeta.mojang.com/mc/game/version_manifest.json'

if sys.platform.startswith('win'): system = 'windows'
elif sys.platform.startswith('darwin'): system = 'osx'
elif sys.platform.startswith('linux'): system = 'linux'
else: system = None

def download_version_manifest(update_cache=False):
    local_path = get_moved_file(VERSION_MANIFEST)
    if not os.path.exists(local_path) or update_cache:
        cached_download(VERSION_MANIFEST)
    return local_path

def load_version_manifest():
    local_path = get_moved_file(VERSION_MANIFEST)
    with open(local_path) as f:
        manifest = json.load(f)
        f.close()
    return manifest

def get_latest_version(version_type='release'):
    version_manifest = load_version_manifest()
    return version_manifest['latest'][version_type]

def get_version_meta_path(version_id='1.12.2'):
    version_manifest = load_version_manifest()
    for version_meta in version_manifest['versions']:
        if version_meta['id'] == version_id:
            return version_meta['url']

def download_version_meta(version_id='1.12.2', update_cache=False):
    version_meta_path = get_version_meta_path(version_id)
    local_path = get_moved_file(version_meta_path)
    if not os.path.exists(local_path) or update_cache:
        cached_download(version_meta_path)
    return local_path

def load_version_meta(version_id='1.12.2'):
    version_meta_path = get_version_meta_path(version_id)
    local_path = get_moved_file(version_meta_path)
    with open(local_path) as f:
        meta = json.load(f)
        f.close()
    return meta

def download_version_client(version_id='1.12.2', update_cache=False):
    meta = load_version_meta(version_id)
    local_path = get_moved_file(meta['downloads']['client']['url'])
    if not os.path.exists(local_path) or update_cache:
        cached_download(meta['downloads']['client']['url'])
    return local_path

def download_version_server(version_id='1.12.2', update_cache=False):
    meta = load_version_meta(version_id)
    local_path = get_moved_file(meta['downloads']['server']['url'])
    if not os.path.exists(local_path) or update_cache:
        cached_download(meta['downloads']['server']['url'])
    return local_path

def get_versions(version_type:None|str=None):
    manifest = load_version_manifest()
    out = []
    for i in manifest['versions']:
        if not version_type:
            out.append(i['id'])
            continue
        if i['type'] == version_type:
            out.append(i['id'])
            continue
    return out

def download_version_libraries(version_id='1.12.2', update_cache=False):
    meta = load_version_meta(version_id)
    out_paths = []
    for library_meta in meta['libraries']:
        if 'artifact' in library_meta['downloads']:
            local_path = get_moved_file(library_meta['downloads']['artifact']['url'])
            out_paths.append(local_path)
            if not os.path.exists(local_path) or update_cache:
                cached_download(library_meta['downloads']['artifact']['url'])
        if 'classifiers' in library_meta['downloads']:
            for classifier in library_meta['downloads']['classifiers']:
                local_path = get_moved_file(library_meta['downloads']['classifiers'][classifier]['url'])
                out_paths.append(local_path)
                if not os.path.exists(local_path) or update_cache:
                    cached_download(library_meta['downloads']['classifiers'][classifier]['url'])
    return out_paths

def get_version_libraries(version_id='1.12.2'):
    meta = load_version_meta(version_id)
    out_paths = []
    for library_meta in meta['libraries']:
        if 'artifact' in library_meta['downloads']:
            local_path = get_moved_file(library_meta['downloads']['artifact']['url'])
            out_paths.append(local_path)
        if 'classifiers' in library_meta['downloads']:
            if f'natives-{system}' in library_meta['downloads']['classifiers']:
                local_path = get_moved_file(library_meta['downloads']['classifiers'][f'natives-{system}']['url'])
                out_paths.append(local_path)
    return out_paths

def get_version_client_jar(version_id='1.12.2'):
    meta = load_version_meta(version_id)
    url = meta['downloads']['client']['url']
    path = get_moved_file(url)
    return path

def get_version_server_jar(version_id='1.12.2'):
    meta = load_version_meta(version_id)
    url = meta['downloads']['server']['url']
    path = get_moved_file(url)
    return path
